<?php

namespace RedMVC\Feed;
use RedMVC;

/**
 * Writer class
 *
 * @category    RedMVC
 * @package     Feed 
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Writer{

    const FEED_TYPE_ATOM_10 = 0;
    const FEED_TYPE_RSS_09  = 1;
    const FEED_TYPE_RSS_20  = 2;
    
    public static function getXml(RedMVC\Feed $feed, $type = self::FEED_TYPE_ATOM_10){
        if($type === self::FEED_TYPE_ATOM_10){
            return self::feedToAtom($feed);
        }
        else if($type === self::FEED_TYPE_RSS_09){
            return self::feedToRss09($feed);
        }
        else if($type === self::FEED_TYPE_RSS_20){
            return self::feedToRss20($feed);
        }
    }
    
    public static function feedToAtom(RedMVC\Feed $feed){    
        $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
        $xml .= '<feed xmlns="http://www.w3.org/2005/Atom">'."\n";

        $xml .= '<title>'.$feed->title.'</title>'."\n";
        $xml .= '<subtitle>'.$feed->description.'</subtitle>'."\n";
        $xml .= '<link href="'.$feed->link.'" />'."\n";
        if(null !== $feed->linkSelf){
            $xml .= '<link rel="self" href="'.$feed->linkSelf.'" />'."\n";
        }
        $xml .= '<updated>'.date(DATE_ATOM, self::_getTime($feed->date)).'</updated>'."\n";
        if(null !== $feed->author){
            $xml .= '<author>'."\n";
            $xml .= '  <name>'.$feed->author.'</name>'."\n";
            $xml .= '</author>'."\n";
        }
        $xml .= '<id>'.$feed->link.'</id>'."\n\n";

        $entries = $feed->getEntries();
        foreach($entries as $entry){
            $xml .= '<entry>'."\n";
            $xml .= '  <title>'.$entry->title.'</title>'."\n";
            $xml .= '  <link href="'.$entry->link.'" />'."\n";
            $xml .= '  <id>'.$entry->link.'</id>'."\n";
            $xml .= '  <updated>'.date(DATE_ATOM, self::_getTime($entry->date)).'</updated>'."\n";
            $xml .= '  <summary>'.$entry->summary.'</summary>'."\n";
            if(null !== $entry->image){
                $imageUrl = RedMVC\File::toBrowser($entry->image, true);
                $xml .= '  <link rel="enclosure" type="'.self::_imgType($entry->image).'" href="'.$imageUrl.'" />'."\n";
            }
            $xml .= '</entry>'."\n\n";
        }
        $xml .= '</feed>';
        return $xml;
    }
    
    public static function feedToRss09(RedMVC\Feed $feed){
        $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
        $xml .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://my.netscape.com/rdf/simple/0.9/">'."\n";
 
        $xml .= '<channel>'."\n";
        $xml .= '  <title>'.$feed->title.'</title>'."\n";
        $xml .= '  <link>'.$feed->link.'</link>'."\n";
        $xml .= '  <description>'.$feed->description.'</description>'."\n";        
        $xml .= '</channel>'."\n\n";

        $entries = $feed->getEntries();
        foreach($entries as $entry){
            $xml .= '<item>'."\n";
            $xml .= '  <title>'.$entry->title.'</title>'."\n";
            $xml .= '  <link>'.$entry->link.'</link>'."\n";
            $xml .= '  <description>'.$entry->summary.'</description>'."\n";
            $xml .= '</item>'."\n\n";
        }
        $xml .= '</rdf:RDF>';
        return $xml;
    }
    
    public static function feedToRss20(RedMVC\Feed $feed){
        $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
        $xml .= '<rss version="2.0">'."\n";

        $xml .= '<channel>'."\n";
        $xml .= '<title>'.$feed->title.'</title>'."\n";
        $xml .= '<description>'.$feed->description.'</description>'."\n";
        $xml .= '<link>'.$feed->link.'</link>'."\n";
        $xml .= '<pubDate>'.date(DATE_RSS, self::_getTime($feed->date)).'</pubDate>'."\n";

        $entries = $feed->getEntries();
        foreach($entries as $entry){
            $xml .= '<item>'."\n";
            $xml .= '  <title>'.$entry->title.'</title>'."\n";
            $xml .= '  <link>'.$entry->link.'</link>'."\n";
            $xml .= '  <description>'.$entry->summary.'</description>'."\n";            
            $xml .= '  <guid>'.$entry->link.'</guid>'."\n";
            $xml .= '  <pubDate>'.date(DATE_RSS, self::_getTime($entry->date)).'</pubDate>'."\n";
            if(null !== $entry->image){
                $imageUrl = RedMVC\File::toBrowser($entry->image, true);
                $xml .= '  <enclosure url="'.$imageUrl.'" type="'.self::_imgType($entry->image).'" length="'.filesize($entry->image).'" />'."\n";
            }
            $xml .= '</item>'."\n\n";
        }
        $xml .= '</channel>'."\n";
        $xml .= '</rss>';
        return $xml;
    }

    protected static function _imgType($imgUrl){
        $type = exif_imagetype($imgUrl);
        return image_type_to_mime_type($type);
    }

    protected static function _getTime($date){
        if(is_int($date)){
            return $date;
        }
        return strtotime($date);
    }
    
}